//
//  RegistController.m
//  Learncloud
//
//  Created by 郭明皓 on 16/6/20.
//  Copyright © 2016年 郭明皓. All rights reserved.
//

#import "RegistController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "Check.h"
@interface RegistController ()
@property (weak, nonatomic) IBOutlet UITextField *TelText;
@property (weak, nonatomic) IBOutlet UITextField *PasswordText;
@property (weak, nonatomic) IBOutlet UITextField *PasswordAgainText;

@end

@implementation RegistController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)Succeed:(id)sender {
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = _TelText.text;// 设置用户名
    user.password =  _PasswordText.text;// 设置密码
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//    [AVUser requestLoginSmsCode:_TelText.text withBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // 注册成功
            NSString *CheckPassword = [Check checkPassword:_PasswordText.text];

                                if (CheckPassword) {
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                                   message:@"密码安全性太低"
                                                                            preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                          handler:^(UIAlertAction * action) {}];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                    return;
                }
                else {
                    AVObject *todo = [AVObject objectWithClassName:@"Todo"];
                    [todo setObject:_TelText.text forKey:@"Tel"];
                    [todo setObject:_PasswordText.text forKey:@"password"];
                    [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        NSLog(@"存储");
                        if (succeeded) {
                            // 存储成功
                            NSLog(@"存储成功");
                            NSLog(@"%@",todo.objectId);
                        } else {
                            // 失败的话，请检查网络环境以及 SDK 配置是否正确
                            NSLog(@"%@", error);
                        }
                    }];
                    
                    [self.navigationController popViewControllerAnimated:YES];
                }

        } else {
            // 失败的原因可能有多种，常见的是用户名已经存在。
            NSLog(@"注册失败%@", error);
            if ([_TelText.text isEqualToString:@""]||_TelText.text ==nil ||[_PasswordText.text isEqualToString:@""]||_PasswordText.text == nil) {
                NSLog(@"密码不能为空");
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                               message:@"用户名或密码不能为空"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {}];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                return;
            }
        }
    }];
    
    
}
















//- (IBAction)Chack:(id)sender {
//    
//    [self chacks];
//    [AVUser verifyMobilePhone:_TelText.text withBlock:^(BOOL succeeded, NSError *error) {
//        //验证结果
//        if (succeeded) {
//            [AVOSCloud requestSmsCodeWithPhoneNumber:_TelText.text
//                                             appName:@"Learncloud"
//                                           operation:@"发送验证码"
//                                          timeToLive:10
//                                            callback:^(BOOL succeeded, NSError *error) {
//                                                if (succeeded) {
//                                                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"发送成功" delegate:nil  cancelButtonTitle:nil otherButtonTitles:nil];
//                                                    [alert show];
//                                                    
//                                                    // 发送成功
//                                                    //短信格式类似于：
//                                                    //您正在{某应用}中进行{具体操作名称}，您的验证码是:{123456}，请输入完整验证，有效期为:{10}分钟
//                                                }
//                                            }];
//            
//        } else {
//            NSLog(@"%@", error);
//        }
//    }];
//}
//- (void)chacks {
//    AVUser *user = [AVUser user];
////    user.username = @"hjiang";
//    user.password =  _PasswordText.text;
//    user.mobilePhoneNumber = _TelText.text;
//    NSError *error = nil;
//    [user signUp:&error];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
